﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.UIElements.UxmlAttributeDescription;

public class MyPagePanel : AppUI
{
    public NetworkImage iconImg;
    public Text nameText;
    public Text statusText;
    public Text myApplyNum;
    public Text myLikeNum;
    public Text followersNum;
    public Text myGuestNum;
    public Text myCoinNum;
    public Text myMomentNum;
    public Text memberText;
    public Text memberTimeEndText;

    public void Init()
    {
        GameDebug.Log($"my page init");

        var user = MGame.instance.MUser;

        nameText.text = user.name;
        var icon = user.userInfo.icon;
        iconImg.Init(icon);
        statusText.text = "积极营业";

        myApplyNum.text = user.myApplyNum.ToString();
        myLikeNum.text = user.belikeNum.ToString();
        followersNum.text = user.followersNum.ToString();
        myGuestNum.text = user.guestNum.ToString();
        myMomentNum.text = user.moments.Count.ToString();
        RefreshDiamond(user.diamond);

        bool member = true;
        if (member)
        {
            memberText.text = "已开通大会员 拥有15项权益";
            memberTimeEndText.text = $"有效期到 {ApplyUserPanel.GetDateTimeStrWithTimestamp(user.memberTimeEndDate)}";
        }
        else
        {
            memberText.text = "开通大会员 获取15项权益"; 
            memberTimeEndText.gameObject.SetActive(false);
        }
    }

    // Start is called before the first frame update
    public override void Start()
    {
        base.Start();
    }

    public void RefreshDiamond(int diamond)
    {
        myCoinNum.text = diamond.ToString();
    }
    public void OnClickStatus()
    {
        GameDebug.Log("MyPagePanel->OnClickStatus");
    }
    public void OnClickHomePagePanel()
    {
        GameDebug.Log("MyPagePanel->OnClickHomePagePanel");
        bool detailInfoNotEdit = MGame.instance.MUser.userInfo.height <= 0 || MGame.instance.MUser.userInfo.weight <= 0;

        if (detailInfoNotEdit)
        {
            UIController.ShowUI<EditDetailInfoPanel>();
        }
        else
        {
            var userHome = GetComponentInParent<UIController>().ShowUI<UserHomePanel>();
            UserMomentsInfo userMomentsInfo = new UserMomentsInfo();
            userMomentsInfo.user = MGame.instance.MUser.userInfo;
            UserMoment[] userMoments = MGame.instance.MUser.moments.Map(e =>
            {
                UserMoment userM = new UserMoment();
                userM.Init(MGame.instance.MUser.userInfo, e);
                return userM;
            }).AsArray();

            userMomentsInfo.moments = userMoments;
            userHome.Init(userMomentsInfo);
        }
    }
    public void OnClickMember()
    {
        GameDebug.Log("MyPagePanel->OnClickMember");
    }
    public void OnClickMyCoin()
    {
        GameDebug.Log("MyPagePanel->OnClickMyCoin");
    }
    public void OnClickMyMoments()
    {
        GameDebug.Log("MyPagePanel->OnClickMyMoments");

        if (MGame.instance.MUser.moments.Count > 0)
        {
            var moment = GetComponentInParent<MainPanel>().UIController.ShowUI<UserMomentsPanel>();
            var userMomentsInfo = new UserMomentsInfo();
            userMomentsInfo.user = MGame.instance.MUser.userInfo;
            userMomentsInfo.moments = MGame.instance.MUser.moments.Map(e => new UserMoment(userMomentsInfo.user, e)).AsArray();
            moment.Init(userMomentsInfo);
        }
        else
        {
            GetComponentInParent<UIController>().ShowUI<AddMomentPanel>();
        }
    }
    public void OnClickMyApply()
    {
        GameDebug.Log("MyPagePanel->OnClickMyApply");
        var apply = GetComponentInParent<UIController>().ShowUI<ApplyPanel>();
        apply.Init(new ApplyPanelData() { applies = MGame.instance.MUser.applies});
        apply.Toggle(ApplyPanel.Toggle_Request);
    }
    public void OnClickLike()
    {
        GameDebug.Log("MyPagePanel->OnClickLike");
        var like = GetComponentInParent<UIController>().ShowUI<LikePanel>();
        like.Init(new LikePanelData() { likes = MGame.instance.MUser.likes,beLikes = MGame.instance.MUser.belikes });
    }
    public void OnClickFollow()
    {
        GameDebug.Log("MyPagePanel->OnClickFollow");
        var panel = GetComponentInParent<UIController>().ShowUI<FollowPanel>();

        FollowPanelData panelData = new FollowPanelData();

        RequestServerController.instance.RequestMiniUserInfo(MGame.instance.MUser.followers.ToArray(), (muis) =>
        {
            panelData.fans = muis;

            RequestServerController.instance.RequestMiniUserInfo(MGame.instance.MUser.myFollows.ToArray(), (muis2) =>
            {
                panelData.myFollows = muis2;

                panel.Init(panelData);
            });
        });
    }
    public void OnClickGuest()
    {
        GameDebug.Log("MyPagePanel->OnClickGuest");

        var visit = GetComponentInParent<UIController>().ShowUI<VisitsPanel>();

        VisistsPanelData panelData = new VisistsPanelData();
        panelData.myVisits = MGame.instance.MUser.myVisits;
        panelData.visitors = MGame.instance.MUser.visitors;
        visit.Init(panelData);
    }
    public void OnClickMyVerify()
    {
        GameDebug.Log("MyPagePanel->OnClickMyVerify");
    }
    public void OnClickMyQE()
    {
        GameDebug.Log("MyPagePanel->OnClickMyQE");
    }
    public void OnClickRequirement()
    {
        GameDebug.Log("MyPagePanel->OnClickRequirement");
        var require = GetComponentInParent<UIController>().ShowUI<EditRequirementPanel>();
        require.Init(MGame.instance.MUser.requirement);
    }
    public void OnClickRules()
    {
        GameDebug.Log("MyPagePanel->OnClickRules");
    }
    public void OnClickPrivacy()
    {
        GameDebug.Log("MyPagePanel->OnClickPrivacy");
    }
    public void OnClickHelp()
    {
        GameDebug.Log("MyPagePanel->OnClickHelp");
    }
    public void OnClickSafty()
    {
        GameDebug.Log("MyPagePanel->OnClickSafty");
    }

}
